﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace BSPortal.Usercontrol
{
    public partial class LastestProduct : System.Web.UI.UserControl
    {
        public short CategoryId;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void LoadData()
        {
            switch (CategoryId)
            {
                case 1:
                    lbCategoryName.InnerText = ResourceString.Book;
                    break;
                case 2:
                    lbCategoryName.InnerText = ResourceString.Electronic;
                    break;
                case 3:
                    lbCategoryName.InnerText = ResourceString.House;
                    break;
            }
            MainService mainService = Common.GetClientService();
            var listProduct = mainService.GetListLastestProductByCategoryId(CategoryId).Take(8);

            productRepeater.DataSource = listProduct;
            productRepeater.DataBind();

            if (listProduct.Count() == 0)
                sum_canyoulike_new.Visible = false;
        }

        protected void productRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if (e.Item is RepeaterItem) //for item template
                {
                    Label lblDiscount = (Label)e.Item.FindControl("lblDiscount");
                    Label txtDiscount = (Label)e.Item.FindControl("txtDiscount");
                    Label lblUnit = (Label)e.Item.FindControl("lblUnit");
                    Label lblPrice_Title = (Label)e.Item.FindControl("lblPrice_Title");

                    Product product = ((Product)e.Item.DataItem);
                    if (product.Discount == null || product.Discount == 0)
                    {
                        lblDiscount.Visible = false;
                        txtDiscount.Visible = false;
                        lblUnit.Visible = false;
                        lblPrice_Title.Text = " Giá bán: ";
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}